package com.huasoft.servlet.file;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huasoft.dao.FileDao;
import com.huasoft.domain.Right;

public class DeleteFileServlet {

	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (!testRight(request, response)) {
			request.setAttribute("noright", "没有权限");
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else {
			Integer id = Integer.valueOf(request.getParameter("id"));
			FileDao fileDao = new FileDao();
			boolean update = fileDao.deleteById(id);
			if (update) {
				request.setAttribute("msg_deleteFileSuccess", "删除案卷成功");
			} else {
				request.setAttribute("msg_deleteFileFalse", "删除案卷失败");
			}
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		}
	}

	/**
	 * 判断用户是否有权限执行 修改用户 的操作
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public boolean testRight(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		boolean ownright = false;
		HttpSession session = request.getSession();
		List<Right> rights = (List<Right>) session.getAttribute("rights");
		String rightstr = (String) request.getParameter("right");
		rightstr = new String(rightstr.getBytes("iso8859-1"), "gbk");
		System.out.println(rightstr);
		for (Right right : rights) {
			if (rightstr.equals(right.getName())) {
				ownright = true;
				break;
			}
		}
		return ownright;
	}

}
